let token = sessionStorage.getItem("token");
$(function () {
    let name = sessionStorage.getItem("realname");
    $("#name").append(name);
    loadData(1);
});
//页数
let pages = 1;
function loadData(pageNum)  {
    $.ajax({
        url: testurl + "due/list",
        type: "post",
        headers: {"Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum": pageNum
        }),
        success: function (data) {
            let dbmessage = data.obj;
            ssd = Math.ceil(data.pageTotal / data.pageSize);
            pages = ssd;
            str = "";
            $.each(dbmessage, function (index, values) {
                str += " <tr style='cursor: pointer' onclick='getDuemeg("+values.id+")' class=\"gradeX\">\n" +
                    "                                        <td>" + values.name + "</td>\n" +
                    "                                        <td>" + values.db_num + "</td>\n" +
                    " </tr>";

            });
            $("#show").html(str);
        }
    });
}
//分页
let pageNum = 1;
$(".first").click(function () {
    pageNum = 1;
    loadData(pageNum);
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".last").click(function () {
    pageNum = pages;
    loadData(pageNum);
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".up").click(function () {
    if (pageNum === 1) {
        return false;
    } else {
        pageNum--;
        loadData(pageNum);
    }
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".down").click(function () {
    if (pageNum === pages) {
        return false;
    } else {
        pageNum++;
        loadData(pageNum);
    }
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
// 得到届别详细信息
function getDuemeg(e) {
    $.ajax({
        url: testurl + "due/detail",
        type: "post",
        headers: {"Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "id":e
        }),
        success:function (data) {
            $("#due_name").val(data.obj.name);
            $("#due_id").val(data.obj.id);
        }
    })
}
//添加委员
$("#go").click(function () {
        let name = $("#duename").val();
        $.ajax({
            url: testurl + "due/",
            type: "post",
            headers: {"Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "name":name
            }),
            success:function (data) {
                if (data.success === true) {
                    $('#my-popup').modal('close');
                    $(".users").html("添加届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }else {
                    $('#my-popup').modal('close');
                    $(".users").html("添加届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }
            }
        });


    return false
});
//保存修改届别名称
$("#save").click(function () {
    let name = $("#due_name").val();
    let due_id = $("#due_id").val();
    if (name === "" || due_id === ""){
        $(".users").html("保存届别");
        $(".alert").html("请选择要修改的届别");
        $('#my-alert').modal({target: '#my-alert'});
    } else{
        $.ajax({
            url: testurl + "due/update",
            type: "post",
            headers: {"Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":due_id,
                "name":name
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("修改届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }else {
                    $(".users").html("修改届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }
            }
        });
    }

    return false
});
//删除届别
$("#delete").click(function () {
    let due_id = $("#due_id").val();
    if (due_id === ""){
        $(".users").html("删除届别");
        $(".alert").html("请选择要删除的届别");
        $('#my-alert').modal({target: '#my-alert'});
    } else{
        $.ajax({
            url: testurl + "due/deletes",
            type: "post",
            headers: {"Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "ids":[due_id]
            }),
            success:function (data) {
                if (data.success === true) {
                    $("#due_name").val("");
                    $(".users").html("删除届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }else {
                    $(".users").html("删除届别");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }
            }
        });
    }
    return false
});
//设置当前届
$("#set_current").click(function () {
    let due_id = $("#due_id").val();
    if (due_id === ""){
        $(".users").html("设置当前届");
        $(".alert").html("请选择要设置的届别");
        $('#my-alert').modal({target: '#my-alert'});
    } else{
        $.ajax({
            url: testurl + "due/set_current",
            type: "post",
            headers: {"Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "ids":due_id
            }),
            success:function (data) {
                if (data.success === true) {
                    $("#due_name").val("");
                    $(".users").html("设置当前届");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }else {
                    $(".users").html("设置当前届");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }
            }
        });
    }
    return false
});